﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class QuanTri_QuanLyToBoMon : System.Web.UI.Page
{
    SoLienLacDienTu cls = new SoLienLacDienTu();
    SoLienLacDataContext db = new SoLienLacDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Username"] == null || Session["Username"].ToString() == "")
        {
<<<<<<< .mine
            Response.Redirect("~/GiaoDienClient/TrangChu.aspx");
=======
            // LoadDSGVChuaPhan();
            LoadDSGV();
            LoadToBoMon();
          //  Xoa.Enabled =false;
           // btnHuy.Enabled = true;
           // cboToBoMon.Enabled = true;
            //btnPhanCong.Enabled =true;
>>>>>>> .r6
        }
        else
        {

            if (!IsPostBack)
            {
                // LoadDSGVChuaPhan();
                LoadDSGV();
                LoadToBoMon();
                Xoa.Enabled = false;
                btnHuy.Enabled = true;
                cboToBoMon.Enabled = true;
                btnPhanCong.Enabled = true;
            }
        }
    }
    void LoadDSGV()
    {
        var c = from p in db.CanBoGiaoViens select new { p.MaCanBoGV, p.TenCanBo };

        grvGV.DataSource = c;
        grvGV.DataBind();
    }
    void LoadDSGVChuaPhan()
    {
        grvGV.DataSource = db.gvChuaPhanChuyenMon();
        grvGV.DataBind();

    }
    void LoadToBoMon()
    {
        var c = from p in db.MonHocs select new { p.MaMon,p.TenMon };
        cboToBoMon.DataTextField = "TenMon";
        cboToBoMon.DataValueField = "MaMon";
        cboToBoMon.DataSource = c;
        cboToBoMon.DataBind();
    }
    protected void cboToBoMon_SelectedIndexChanged(object sender, EventArgs e)
    {
        btnPhanCong.Enabled = true;
        var c = from p in db.ToBoMons where p.MaMon == int.Parse(cboToBoMon.SelectedValue.ToString()) select p.MaMon ;
        if (c.Count() != 0)
        {

            btnHuy.Enabled = true;
            Xoa.Enabled = true;
            LoadgrvToBoMon();
        }
    }
    void LoadgrvToBoMon()
    {
        var c = from p in db.ToBoMons where p.MaCanBoGV != null && p.MaMon == int.Parse(cboToBoMon.SelectedValue.ToString()) select new { p.MaMon, p.MaCanBoGV,p.MonHoc.TenMon, p.CanBoGiaoVien.TenCanBo };
     
        GrvChuyen.DataSource = c;
        GrvChuyen.DataBind();
    }
    protected void ckChonTatCa_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < grvGV.Rows.Count; i++)
        {
            CheckBox ck = (CheckBox)grvGV.Rows[i].Cells[0].FindControl("ckChonGV");
            if (ckChonTatCa.Checked == true)
            {
                ck.Checked = true;
              //  btnPhanCong.Enabled = true;
            }
            if (ckChonTatCa.Checked == false)
            {
                ck.Checked = false;
               // btnPhanCong.Enabled = false;
            }
        }
    }
    protected void btnPhanCong_Click(object sender, EventArgs e)
    {
        ToBoMon tbm;
        bool kt = true;
       // var c = from p in db.ToBoMons select p.MaCanBoGV;
        for (int i = 0; i < grvGV.Rows.Count; i++)
        {
            CheckBox ck = (CheckBox)grvGV.Rows[i].Cells[0].FindControl("ckChonGV");
            Label lblMa = (Label)grvGV.Rows[i].Cells[2].FindControl("lblMaGV");
            if (ck.Checked == true)
            {
                tbm = new ToBoMon();
                tbm.MaMon = int.Parse(cboToBoMon.SelectedValue.ToString());
                tbm.MaCanBoGV = lblMa.Text;
                tbm.TenBoMon = cboToBoMon.SelectedItem.Text;
                db.ToBoMons.InsertOnSubmit(tbm);
<<<<<<< .mine
                db.SubmitChanges();
=======
              
>>>>>>> .r6
            }
            db.SubmitChanges();
        }
<<<<<<< .mine
        
        Xoa.Enabled = true;
        btnHuy.Enabled = true;
=======
>>>>>>> .r6
        if (rdoDSCPC.Checked == true)
        {
            LoadDSGVChuaPhan();
        }
        if (rdoDSTCGV.Checked == true || (rdoDSTCGV.Checked == false && rdoDSCPC.Checked == false))
        {
            LoadDSGV();
        }
        LoadgrvToBoMon();
    }
    bool MaTrung()
    {
        bool kt=true;
        //for (int i = 0; i < grvGV.Rows.Count; i++)
        //{
        //    Label Ma = (Label)grvGV.Rows[i].Cells[2].FindControl("lblMaGV");
        //    var c=from p in db.ToBoMons where p.MaCanBoGV==Ma.Text select p.MaCanBoGV;
        //    if (c.Count() != 0)
        //    {
        //        kt = false;
        //        ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('GV này đã được phân công')", true);
        //    }

        //}
        return kt;
    }
    bool KTCheck()
    {
        bool kt = true;
        for (int i = 0; i < GrvChuyen.Rows.Count; i++)
        {
            CheckBox ck = (CheckBox)GrvChuyen.Rows[i].Cells[0].FindControl("ckChonGV");
            if (ck.Checked == false)
            {
                kt = false;
                ScriptManager.RegisterStartupScript(this,this.GetType(),"Alert","alert('Bạn chưa chọn GV để phân')",true);
            }
        }
        return kt;
    }
    protected void btnHuy_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < GrvChuyen.Rows.Count; i++)
        {
            CheckBox ck = (CheckBox)GrvChuyen.Rows[i].Cells[0].FindControl("ChonGV");
            Label Ma = (Label)GrvChuyen.Rows[i].Cells[2].FindControl("lblMaCanBo");
            if (ck.Checked == true)
            {
                ToBoMon tbm = db.ToBoMons.SingleOrDefault(p => p.MaMon == int.Parse(cboToBoMon.SelectedValue.ToString())&&p.MaCanBoGV==Ma.Text);
                db.ToBoMons.DeleteOnSubmit(tbm);
            }
        }
        db.SubmitChanges();
        LoadgrvToBoMon();
        if (rdoDSCPC.Checked == true)
        {
            LoadDSGVChuaPhan();
        }
        if (rdoDSTCGV.Checked == true || (rdoDSTCGV.Checked == false && rdoDSCPC.Checked == false))
        {
            LoadDSGV();
        }
        Xoa.Checked = false;
    }
    protected void Xoa_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < GrvChuyen.Rows.Count; i++)
        {
            CheckBox ck = (CheckBox)GrvChuyen.Rows[i].Cells[0].FindControl("ChonGV");
            if (Xoa.Checked == true)
            {
                ck.Checked = true;
                btnHuy.Enabled = true;
            }
            if (Xoa.Checked == false)
            {
                btnHuy.Enabled = false;
                ck.Checked = false;
            }
        }
    }
    protected void rdoDSTCGV_CheckedChanged(object sender, EventArgs e)
    {
        LoadDSGV();
    }
    protected void rdoDSCPC_CheckedChanged(object sender, EventArgs e)
    {
        LoadDSGVChuaPhan();
    }
    protected void GrvChuyen_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = GrvChuyen.SelectedRow;
        Label lblMaMon = (Label)row.FindControl("lblMaMon");
        Label lblMaGV = (Label)row.FindControl("lblMaCanBo");
        ToBoMon tbm = db.ToBoMons.SingleOrDefault(p => p.MaCanBoGV == lblMaGV.Text && p.MaMon == int.Parse(lblMaMon.Text));
        cboToBoMon.Enabled = false;
        btnHuy.Enabled = false;
        btnPhanCong.Enabled = false;
    }
    protected void btnLuu_Click(object sender, EventArgs e)
    {
        GridViewRow row = GrvChuyen.SelectedRow;
        Label lblMaMon = (Label)row.FindControl("lblMaMon");
        Label lblMaGV = (Label)row.FindControl("lblMaCanBo");
        ToBoMon tbm = db.ToBoMons.SingleOrDefault(p => p.MaCanBoGV == lblMaGV.Text && p.MaMon == int.Parse(lblMaMon.Text));
        tbm.MaMon = int.Parse(cboToBoMon.SelectedValue.ToString());
        tbm.MaCanBoGV = lblMaGV.Text;
        db.SubmitChanges();
        cboToBoMon.Enabled = true; ;
       
    }
    protected void btnTim_Click(object sender, EventArgs e)
    {
        if (rdoDSCPC.Checked == true)
        {
            var c = from p in db.gvChuaPhanChuyenMon() where p.TenCanBo == txtTenGV.Text select p;
            grvGV.DataSource = c;
            grvGV.DataBind();
        }
        if (rdoDSTCGV.Checked == true || (rdoDSTCGV.Checked == false && rdoDSCPC.Checked == false))
        {
            var c = from p in db.CanBoGiaoViens where p.TenCanBo == txtTenGV.Text select new {p.TenCanBo,p.MaCanBoGV };
            grvGV.DataSource = c;
            grvGV.DataBind();
        }
    }
    protected void txtTebBoMon_TextChanged(object sender, EventArgs e)
    {
        //if (txtTebBoMon.Text != "")
        //{
        //    lbtThongBao.Text = "";
        //    btnPhanCong.Enabled = true;
        //}
    }
}
